package gcmiddleware

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

// SetCors 设置跨域
func SetCors(c *gin.Context, m map[string]string) {
	defaultM := map[string]string{
		"Access-Control-Allow-Origin":      "*",
		"Access-Control-Allow-Headers":     "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, Cache-Control, X-Requested-With",
		"Access-Control-Allow-Methods":     "POST, GET, OPTIONS",
		"Access-Control-Allow-Credentials": "true",
		"Access-Control-Max-Age":           "3600",
	}
	for k, v := range m {
		defaultM[k] = v
	}
	for k, v := range defaultM {
		c.Header(k, v)
	}

	if c.Request.Method == "OPTIONS" {
		c.AbortWithStatus(http.StatusNoContent)
	}
}
